package com.investmenttechnologyservices.util;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;

public class OFXgetBanks {
	
	static public Institution[] getAll()
	{
		Institution[] ret = {};
		
		URL ofx;
		HttpURLConnection conn;
		
		try
		{
			ofx = new URL("http://www.ofxhome.com/api.php?all=yes");
		}
		catch (MalformedURLException e)
		{
			System.out.println("Exception: " + e);
			return ret;
		}
		
		// Get result
		try
		{
			conn = (HttpURLConnection) ofx.openConnection();
		}
		catch (IOException e)
		{
			System.out.println("Exception: " + e);
			return ret;
		}
			
		DocumentBuilder builder;
		Document response;
		try
		{
			builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
		}
		catch (ParserConfigurationException e)
		{
			System.out.println("Exception: " + e);
			return ret;
		}
		
		try
		{
			response = builder.parse(conn.getInputStream());
		}
		catch (Exception e)
		{
			System.out.println("Exception: " + e);
			return ret;
		}
		
		// Get name
		NodeList n = response.getElementsByTagName("institutionid");
		ret = new Institution[n.getLength()];
		Integer  i;
		for (i = 0; i < n.getLength(); i++)
		{
			Element e = (Element) n.item(i);
			
			ret[i] = new Institution();
			ret[i].setName(e.getAttribute("name"));
			ret[i].setId(e.getAttribute("id"));
		}
		
		return ret;
	}

	static public Institution downloadInstitution(String id) throws IOException
	{
		URL ofx;
		HttpURLConnection conn;
		Institution i = new Institution();
		
		ofx = new URL("http://www.ofxhome.com/api.php?lookup=" + id);

		// Get result
		conn = (HttpURLConnection) ofx.openConnection();
		
		DocumentBuilder builder;
		Document response;
		try
		{
			builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
		}
		catch (ParserConfigurationException e)
		{
			System.out.println("Exception: " + e);
			return null;
		}
		
		try
		{
			response = builder.parse(conn.getInputStream());
		}
		catch (Exception e)
		{
			System.out.println("Exception: " + e);
			return null;
		}
		
		// Get name
		NodeList n = response.getElementsByTagName("name");
		if (n.getLength() > 0)
		{
			Text a = (Text) n.item(0).getFirstChild();
			if (a != null)
			{
				i.setName(a.getNodeValue());
			}
		}

		// Get fid
		n = response.getElementsByTagName("fid");
		if (n.getLength() > 0)
		{
			Text a = (Text) n.item(0).getFirstChild();
			if (a != null)
			{
				i.setFId(a.getNodeValue());
			}
		}

		// Get org
		n = response.getElementsByTagName("org");
		if (n.getLength() > 0)
		{
			Text a = (Text) n.item(0).getFirstChild();
			if (a != null)
			{
				i.setOrg(a.getNodeValue());
			}
		}

		// Get url
		n = response.getElementsByTagName("url");
		if (n.getLength() > 0)
		{
			Text a = (Text) n.item(0).getFirstChild();
			if (a != null)
			{
				i.setUrl(a.getNodeValue());
			}
		}

		// Get brokerId
		n = response.getElementsByTagName("brokerid");
		if (n.getLength() > 0)
		{
			Text a = (Text) n.item(0).getFirstChild();
			if (a != null)
			{
				i.setBrokerId(a.getNodeValue());
			}
		}

		return i;
	}

}
